{extend name="public:base"}
{block name='css'}
{/block}

{block name='body'}
<div class="view view-main white-navbar white-toolbar">
    <div data-name="home" class="page">

        <div class="navbar">
            <div class="navbar-inner">
                <div class="left"><a href="{$my_prev_url|default='/'}" class="external"><i class="iconfont icon-fanhui"></i></a></div>
                <div class="center"><span>充值</span></div>
                <div class="right"></div>
            </div>
        </div>

        <div class="page-content padding-bottom50">

            <form name="form" action=""  method="post" id="form">

                <div class="chongzhi">
                    <div class="item-title item-label">USDT地址</div>
                    <input value="{:config('usdt_addr')}" style="border:none;background: none;text-align: right;" disabled/>
                </div>
                <div class="chongzhi">
                    <div class="item-title item-label">USDT收款码</div>
                    <img src="{:config('usdt_img')}"  class="img-loading show_img" style="width: 100px;height:100px;" data-src='{:config("usdt_img")}'/>

                </div>
                <div class="chongzhi">
                    
                    <span class="sp1">￥</span>
                    <input type="number" step="0.01" min="1" max="999999" name="amount" placeholder="请输入充值金额..."/>
                </div>
                <div class="chongzhi">
                    <div class="item-title item-label">打款凭证</div>
                    <div class="item-input-wrap">
                        <div class="dantu">
                            <img src="{$info.license|default='__PUBLIC__/img/a7.png'}" class="imgs"/>
                            <img src="__PUBLIC__/img/loading.gif"  class="img-loading"/>
                            <input type="file" class="js-img" />
                            <input type="hidden" name="img" value="" class="js-int-img" />
                        </div>
                    </div>
                </div>
                <!-- <p style="padding: 0 10px;color:red;font-size: 12px;">注意：充值金额不可提现，不可消费！！！</p> -->

                <div class="block">
                    <p class="row segmented-round">
                        <button class="col button button-big button-fill " style="background:#66ba61;">提交</button>
                    </p>
                </div>
            </form>

        </div>


    </div>
</div>
{/block}

{block name="js"}
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
    function jsApiCall(params) {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            params,
            function(res){
                WeixinJSBridge.log(res.err_msg);
                if(res.err_msg == 'get_brand_wcpay_request:ok'){
                    messageToast('支付成功');
                    setTimeout(function(){
                        self.location = "{:url('wallet')}";
                    },1000);
                } else {
                    messageToast('调起支付失败');
                }
            }
        );
    }

    function callpay(params)
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }else{
            jsApiCall(params);
        }
    }

    var ajaxOn = false ;
    $().ready(function () {
        $('.js-img').change(function(event){
            ajaxImgFile('.imgs', '.js-int-img', event);
        });
        $("#form").validate({
            rules: {
                money: {
                    required: true,
                    number:true ,
                    min:1
                },
                img : "required",
            },
            messages: {
                money: {
                    required: "请输入充值数量",
                    number: "请输入正确的金额",
                    min: "最少充值数量1"
                },
                img : "请上传凭证",

            },
            showErrors: function (errorMap, errorList) {
                $.each(errorList, function (i, v) {
                    messageToast( v.message );
                    return false;
                });
            },
            onfocusout: false,
            onsubmit : true ,
            onkeyup : false ,
            onclick : false ,
            focusInvalid:false,
            submitHandler:function(form){
                if (ajaxOn) {
                    return false ;
                }
                ajaxOn = true ;
                $.post('/recharge', $('#form').serialize(), function(data){
                    //测试
                    // if (/SUCCESS/.test(data)) {
                    //     messageToast('支付成功');
                    //     setTimeout(function(){
                    //         self.location = "{:url('wallet')}";
                    //     },1000);
                    // } else {
                    //     messageToast('支付失败');
                    // }
                    // return false;

                    if (data.status == 'y') {
                        setTimeout(function () {
                            self.location = "{:url('recharge_details')}";
                        }, 1000);
                    } else {
                        messageToast(data.info);
                    }
                    ajaxOn = false;
                // });
                }, 'json');

                return false ;

            }
        });
    });

    function ajaxImgFile( imgObj, inputObj, event ){
        // if(! $(inputObj).val()) return false ;
        app.dialog.preloader('上传中...');
        var files = event.target.files || event.dataTransfer.files;
        var formData = new FormData();
        formData.append('imgFile', files[0]);
        var ajax = new XMLHttpRequest();
        ajax.open("POST", "{:url('index/saveImg')}", true);
        ajax.send(formData);
        ajax.onreadystatechange = function() {
            if (ajax.readyState == 4) {
                if (ajax.status >= 200 && ajax.status < 300 || ajax.status == 304) {
                    var data = JSON.parse(ajax.responseText);
                    if (data.status == 'y') {
                        //修改img为上传后的图片地址
                        if ( imgObj ) {
                            $(imgObj).attr('src',data.data.src);
                        }
                        //给传值的input赋值
                        if ( inputObj ) {
                            $(inputObj)	.val(data.data.src);
                        }
                        var toastCenter = app.toast.create({
                            text: data.info,
                            position: 'center',
                            closeTimeout: 2000
                        });
                        toastCenter.open();
                    } else {
                        var toastCenter = app.toast.create({
                            text: data.info,
                            position: 'center',
                            closeTimeout: 2000
                        });
                        toastCenter.open();
                    }
                    app.dialog.close();
                }
            }
        }

    }
</script>
{/block}